{ <description>

  Copyright (C) 2011 Anton A. Lindeberg lindeberg.a@gmail.com

  This source is free software; you can redistribute it and/or modify it under
  the terms of the GNU General Public License as published by the Free
  Software Foundation; either version 2 of the License, or (at your option)
  any later version.

  This code is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  details.

  A copy of the GNU General Public License is available on the World Wide Web
  at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
  to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  MA 02111-1307, USA.
}
unit u_optionsfrm;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  ComCtrls, StdCtrls, Buttons, SynEdit, IniFiles;

type

  { Tf_Options }

  Tf_Options = class(TForm)
    bt_Cancel: TBitBtn;
    bt_save: TBitBtn;
    bt_edt: TSpeedButton;
    cb_fontsize: TComboBox;
    chb_gutter: TCheckBox;
    chb_SQL_Compl: TCheckBox;
    chb_quality: TCheckBox;
    cb_font: TComboBox;
    gb_font: TGroupBox;
    GroupBox2: TGroupBox;
    lb_font: TLabel;
    lb_FontSize: TLabel;
    lbl_opt: TLabel;
    lb_lang: TListBox;
    pc_option: TPageControl;
    bt_lng: TSpeedButton;
    edt_simp: TSynEdit;
    ts_language: TTabSheet;
    ts_editor: TTabSheet;
    procedure bt_CancelClick(Sender: TObject);
    procedure bt_edtClick(Sender: TObject);
    procedure bt_lngClick(Sender: TObject);
    procedure bt_saveClick(Sender: TObject);

    procedure cb_fontSelect(Sender: TObject);
    procedure cb_fontsizeChange(Sender: TObject);
    procedure chb_gutterEditingDone(Sender: TObject);
    procedure chb_qualityEditingDone(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FindLanguages(path, ext: string);

    procedure lb_langClick(Sender: TObject);
    procedure SetLng(Sender: TObject);


  private
    { private declarations }
  public
    { public declarations }
  end;

var
  f_Options: Tf_Options;
  aBtn: TSpeedButton;


implementation

uses uMain, sslib;

{$R *.lfm}

{ Tf_Options }




procedure Tf_Options.FindLanguages(path, ext: string);
var
  SR: TSearchRec;
  i: integer;
begin
  i := FindFirst(path + ext, faAnyFile, SR);
  while i = 0 do
  begin
    lb_lang.Items.Add(sr.Name);
    i := FindNext(SR);
  end;
  FindClose(SR);
end;



procedure Tf_Options.lb_langClick(Sender: TObject);
begin
  Language := lb_lang.Items[lb_lang.ItemIndex];
  SetLng(Sender);
  lbl_opt.Caption := Localize('Language');
  //f_main.SetLng(Sender);
end;


procedure Tf_Options.SetLng(Sender: TObject);
begin
  LoadLocalize(Language);
  bt_lng.Caption         := Localize('Language');
  Caption                := Localize('Options');
  bt_edt.Caption         := Localize('Editor');
  bt_cancel.Caption      := Localize('Cancel');
  bt_save.Caption        := Localize('Save');
  chb_SQL_Compl.Caption  := Localize('SQL Completion');

  gb_font.Caption        := Localize('Font');
  chb_quality.Caption    := Localize('Antialiasing');
  lb_FontSize.Caption    := Localize('Font size')+':';
  lb_Font.Caption        := Localize('Font name')+':';


end;


procedure Tf_Options.FormShow(Sender: TObject);
begin
  SetLng(Sender);
  aBtn := bt_lng;
  chb_quality.Checked := opt.AA;
  chb_gutter.Checked := opt.Gutter;
  chb_SQL_Compl.Checked:=opt.SQLCompletion;

  cb_fontsize.Text := IntToStr(opt.FontSize);
  cb_font.Font.Name := opt.FontName;
  cb_font.Text := opt.FontName;
  edt_simp.Font.Name:=opt.FontName;
  edt_simp.Font.Size:=opt.FontSize;
  edt_simp.Gutter.Visible:=opt.Gutter;
  chb_qualityEditingDone(Sender);

end;


procedure Tf_Options.bt_lngClick(Sender: TObject);
var
  s: string;
begin
  lb_lang.Clear;
  pc_option.ActivePage.Hide;
  aBtn.Flat := True;
  bt_lng.Flat := False;
  aBtn := bt_lng;
  S := ExtractFileDir(ParamStr(0)) + DirectorySeparator + 'languages' +
    DirectorySeparator;
  FindLanguages(s, '*.lng');
  ts_language.Show;
  lbl_opt.Caption := Localize('Language');

end;



procedure Tf_Options.bt_saveClick(Sender: TObject);
var
  cfg: TiniFile;
begin
  cfg := TIniFile.Create(path+'pgdev.cfg');
  cfg.DeleteKey('Default', 'Language');
  cfg.WriteString('Default', 'Language', Language);
  cfg.DeleteKey('Editor', 'FontName');
  cfg.WriteString('Editor', 'FontName', cb_font.Text);
  cfg.DeleteKey('Editor', 'FontSize');
  cfg.WriteInteger('Editor', 'FontSize', StrToInt(cb_fontsize.Text));
  cfg.DeleteKey('Editor', 'Gutter');
  cfg.WriteBool('Editor', 'Gutter', chb_Gutter.Checked);
  cfg.DeleteKey('Editor', 'antialiasing');
  cfg.WriteBool('Editor', 'antialiasing', chb_quality.Checked);

  {!!!!!!!! Не удаляются ключи! !!!!!!!!!!!!}
  cfg.DeleteKey('Completion', 'DBOCompl');

  cfg.DeleteKey('Completion', 'SQLCompl');
  cfg.WriteBool('Completion', 'SQLCompl', chb_SQL_Compl.Checked);


  cfg.Free;
  f_main.ReadIni();
  f_main.SetOptions();
  f_main.SetLng();
  f_Options.Close;
end;



procedure Tf_Options.cb_fontSelect(Sender: TObject);
begin
 cb_font.Font.Name:=cb_font.Text;
 Edt_simp.Font.Name:=cb_font.Text;
end;




procedure Tf_Options.cb_fontsizeChange(Sender: TObject);
begin
  edt_simp.Font.Size := (StrToInt(cb_fontsize.Text));
end;


procedure Tf_Options.chb_gutterEditingDone(Sender: TObject);
begin
  if chb_gutter.Checked = True then
  begin
 {$ifdef Windows}
    edt_simp.Gutter.Visible := True;
  {$endif}
  end
  else
    edt_simp.Gutter.Visible := False;

end;


procedure Tf_Options.chb_qualityEditingDone(Sender: TObject);
begin
  if chb_quality.Checked = False then
  begin
 {$ifdef Windows}
    edt_simp.Font.Quality := fqClearType;
  {$endif}
  end
  else
  begin
    edt_simp.Font.Quality := fqDefault;
  end;

end;


procedure Tf_Options.bt_edtClick(Sender: TObject);
begin
  aBtn.Flat := True;
  bt_edt.Flat := False;
  aBtn := bt_edt;
  ts_editor.Show;
  cb_font.Items := Screen.Fonts;
  cb_fontsize.Text := IntToStr(edt_simp.Font.Size);
  lbl_opt.Caption := Localize('Editor Options');
end;

procedure Tf_Options.bt_CancelClick(Sender: TObject);
begin
  f_Options.Close;
end;


end.

